﻿using System.IO;
using System.IO.Compression;

namespace MyConsoleApp.Code
{
    /// <summary>
    /// 压缩和解压类
    /// </summary>
    public class ZipHelper
    {
        private ZipHelper() { }

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="inBytes"></param>
        /// <returns></returns>
        public static MemoryStream Compress(byte[] inBytes)
        {
            MemoryStream outStream = new MemoryStream();
            using (MemoryStream intStream = new MemoryStream(inBytes))
            {
                using (GZipStream Compress = new GZipStream(outStream, CompressionMode.Compress))
                {
                    intStream.CopyTo(Compress);
                }
            }

            return outStream;
        }

        //压缩
        public static byte[] CompressBytes(byte[] inBytes)
        {
            return Compress(inBytes).ToArray();
        }

        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="inStream"></param>
        /// <returns></returns>
        public static byte[] Decompress(MemoryStream inStream)
        {
            byte[] result = null;
            MemoryStream compressedStream = new MemoryStream(inStream.ToArray());

            using (MemoryStream outStream = new MemoryStream())
            {
                using (GZipStream Decompress = new GZipStream(compressedStream, CompressionMode.Decompress))
                {
                    Decompress.CopyTo(outStream);
                    result = outStream.ToArray();
                }
            }

            return result;
        }

        //解压
        public static byte[] DecompressBytes(byte[] inBytes)
        {
            return Decompress(new MemoryStream(inBytes));
        }
    }
}
